<?php 
if(KT!='KhiThe') exit;
class users_controller extends users_model{
		
	public function __construct()
	{
		global $lang;
		$lang['sms']='';
		$this->lang = $lang;
		parent::__construct();
		parent::addObject('khithe_template');
		parent::addObject('khithe_request');
		parent::addObject('khithe_string');
		parent::addObject('khithe_paging');
		parent::addObject('khithe_captcha');
	}
	
	public function index_action()
	{
		$arrBlockHtml = $this->khithe_display('index');
		$resultUser = parent::getUsers(trim($_SESSION['Username']));
		$row_Users = mysql_fetch_assoc($resultUser);
		$arrUser = array(
			"cell_ID"=>$row_Users['ID'],
			"cell_1"=>$row_Users['Username'],
			"cell_2"=>$row_Users['Fullname'],
			"cell_3"=>$row_Users['Email'],
			"cell_4"=>$row_Users['Mobi'],
			"cell_5"=>$row_Users['CMND'],
			"cell_6"=>$row_Users['Address'],			
		);
		$resultHtml = $this->khithe_assign($arrBlockHtml,$arrUser);	
		mysql_free_result($resultUser);
		$strHtmlContent = $this->khithe_assign($resultHtml, $this->lang);
		return $strHtmlContent;
	}
	
	public function register_action()
	{	
		if(!empty($_SESSION['ID']))
			$this->gotoload(url_base);
		else{
			if($_POST['acUser']!='')
			{
				$Username = parent::checkStrMySQL($_POST['txt_users']);
				$Email = parent::checkStrMySQL($_POST['txt_Email']);
				if($Username==''){
					$this->lang['sms'] = $this->lang['EmptyUser'];
					
				}else if(parent::getUsername($Username)>0) {
					$this->lang['sms'] = $this->lang['YesUser'];
				
				}elseif(parent::getMail($Email)>0) {
					$this->lang['sms'] = $this->lang['YesMail'];
					
				}else if($_POST['captcha']!=$_SESSION['Captcha']){
					$this->lang['sms'] = $this->lang['notEmptyCaptcha'];
				
				}else{
					$done = parent::updateInsertUser($_POST);
					if($done){ 
						$this->sms($this->lang['addDone']);
						$this->gotoload(url_base);
					}
				}
			}
			
			$this->lang['titlePageEditor'] = $this->lang['addTitle'];
			$this->lang['only'] ='';
			$this->lang['txt_usersValue'] = $_POST['txt_users'];
			$this->lang['txt_passValue'] = '';
			$this->lang['txt_pass2Value'] = '';
			$this->lang['txt_FullnameValue'] =$_POST['txt_Fullname'];
			$this->lang['txt_PhoneValue'] =$_POST['txt_Phone'];
			$this->lang['txt_EmailValue'] =$_POST['txt_Email'];
			$this->lang['txt_YahooValue'] =$_POST['txt_Yahoo'];
			$this->lang['txt_MobiValue'] =$_POST['txt_Mobi'];
			$this->lang['txt_CMNDValue'] =$_POST['txt_CMND'];
			$this->lang['txt_AddressValue'] =$_POST['txt_Address'];
			$this->lang['active'] ='add';
			$this->lang['usersID'] ='';
			
			$strHtmlContent = $this->khithe_display('editor');
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
	}
	
	public function edit_action()
	{
		if(empty($_SESSION['ID']))
			$this->gotoload(url_base);
		else{
			
			if($_POST['acUser']!='')
			{
				$Username = parent::checkStrMySQL($_POST['txt_users']);
				$user_old = parent::checkStrMySQL($_POST['user_old']);
				$Email = parent::checkStrMySQL($_POST['txt_Email']);
				$mail_old = parent::checkStrMySQL($_POST['mail_old']);
				
				if($Username==''){
					$this->lang['sms'] = $this->lang['EmptyUser'];
					
				}else if($user_old!=$Username && parent::getUsername($Username)>0) {
					$this->lang['sms'] = $this->lang['YesUser'];
				
				}elseif($mail_old!=$Email && parent::getMail($Email)>0) {
					$this->lang['sms'] = $this->lang['YesMail'];
					
				}else if($_POST['captcha']!=$_SESSION['Captcha']){
					$this->lang['sms'] = $this->lang['notEmptyCaptcha'];
					
				}else{
					$done = parent::updateInsertUser($_POST);
					if($done) $this->gotoload(url_base.'users/');
				}
			}
			
			$resultUser = parent::getUserID($_SESSION['ID']);
			$row_Users = mysql_fetch_assoc($resultUser);
			$strHtmlContent = $this->khithe_display('editor');
			$this->lang['titlePageEditor'] = $this->lang['editTitle'];
			$this->lang['only'] =' readonly="readonly" style="border:0px; background:#EFEFEF;" ';
			$this->lang['txt_usersValue'] = $_POST['txt_users']!=''?$_POST['txt_users']:$row_Users['Username'];
			$this->lang['txt_passValue'] = '';
			$this->lang['txt_pass2Value'] = '';
			$this->lang['select_Admin'] =($row_Users['Mod']=='Admin' || $_POST['mod']=='Admin'?' selected="selected" ':'');
			$this->lang['select_Mod'] =($row_Users['Mod']=='Members' || $_POST['mod']=='Members'?' selected="selected" ':'');
			$this->lang['select_Cus'] =($row_Users['Mod']=='Customer'|| $_POST['mod']=='Customer'?' selected="selected" ':'');
			$this->lang['txt_FullnameValue'] = $_POST['txt_Fullname']!=''?$_POST['txt_Fullname']:$row_Users['Fullname'];
			$this->lang['txt_PhoneValue'] = $_POST['txt_Phone']!=''?$_POST['txt_Phone']:$row_Users['Phone'];
			$this->lang['txt_EmailValue'] = $_POST['txt_Email']!=''?$_POST['txt_Email']:$row_Users['Email'];
			$this->lang['txt_YahooValue'] = $_POST['txt_Yahoo']!=''?$_POST['txt_Yahoo']:$row_Users['YahooChat'].'@yahoo.com';
			$this->lang['txt_MobiValue'] = $_POST['txt_Mobi']!=''?$_POST['txt_Mobi']:$row_Users['Mobi'];
			$this->lang['txt_CMNDValue'] = $_POST['txt_CMND']!=''?$_POST['txt_CMND']:$row_Users['CMND'];
			$this->lang['txt_AddressValue'] = $_POST['txt_Address']!=''?$_POST['txt_Address']:$row_Users['Address'];
			$this->lang['active'] ='edit';
			$this->lang['usersID'] =$row_Users['ID'];
			
			$strHtmlContent = $this->khithe_assign($strHtmlContent, $this->lang);
			return $strHtmlContent;
		}
	}
	
	public function loguot_action()
	{	
		$_SESSION['Username']='';
		$_SESSION['Username']='';
		$_SESSION['Fullname']='';
		$_SESSION['Mod']='';
		unset($_SESSION);
		session_destroy();
		return $this->gotoload(url_base);
	}

	public function index_ajax_action()
	{
		if($this->valUrl(1)=='img')
		{			
			return $this->showImage();
			
		}else{
			return 'notOK';
		}
	}

}
?>